package com.loong.common.elasticsearch.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.loong.common.utils.TransformUtils;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.geo.GeoPoint;

import java.io.IOException;
import java.lang.reflect.Type;

@Slf4j
public class GeoPointSerializer implements ObjectSerializer {

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if(object instanceof GeoPoint){
            GeoPointModel transform = TransformUtils.transform(object, GeoPointModel.class);
            serializer.write(transform);
        }else {
            throw new RuntimeException("need GeoPoint type");
        }
    }
}
